Power DI
A lightweight Dependency Injection library. Using es6 and other features, remove unnecessary concepts, easy and convenient to use.
Install
npm i power-di --save
Example
a simple example
import { IocContext } from 'power-di'
class AService { }
const context = IocContext.DefaultInstance
context.register(AService)
const aService = context.get(AService)
inject with key
class AService { }
const context = IocContext.DefaultInstance
context.register(AService, 'XService')
context.get('XService')
use with decorators
import { getDecorators } from '../helper'
const {
register, append, inject, lazyInject, registerSubClass, lazyInjectSubClass
} = getDecorators()
@register()
class AService { }
class SomeClass {
@inject()
private aService: AService
@lazyInject()
private bService: AService
}
context.get(AService)
use in react
import { IocProvider, Component } from 'power-di/react'
@register()
class AService { }
class TestComponent extends Component<{}, {}> {
componentWillMount() {
this.GetComponent(AService)
}
render(): any {
return null
}
}
<IocProvider context={context}>
<TestComponent />
</IocProvider>
collect some kind of object
class A { }
@register(undefined, { regInSuperClass: true })
class B extends A { }
@registerSubClass()
class C extends A { }
@append(A)
class D { }
class LITestService {
@lazyInject({ type: A, subClass: true })
public testService1: A[]
@lazyInjectSubClass({ type: A })
public testService2: A[]
}